<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
// --------------------------------------------------------------------
if (!function_exists('embed_code_parse') ){
    function embed_code_parse($content){

        $regex = '/\[youtube\]http:\/\/(?:www\.)?youtube.com\/watch\?v=([0-9A-Za-z_]+)(?:[^\s?]+)?\[\/youtube\]/';     
        //$regex = '/{jwplayer}<a href=\"http:\/\/(?:www\.)?youtube.com\/watch\?v=([0-9A-Za-z_]+)(?:[^\s?]+)?\">http:\/\/(?:www\.)?youtube.com\/watch\?v=[0-9A-Za-z_]+(?:[^\s?]+)?<\/a>{\/jwplayer}/';
    
        //convert old plugin
        $content = preg_replace('/{jwplayer}<a href=\"http:\/\/(?:www\.)?youtube.com\/watch\?v=([0-9A-Za-z_]+)(?:[^\s?]+)?\">http:\/\/(?:www\.)?youtube.com\/watch\?v=[0-9A-Za-z_]+(?:[^\s?]+)?<\/a>{\/jwplayer}/', "[youtube]http://www.youtube.com/watch?v=$1[/youtube]", $content);
        //exit;
        $player_options = array(
            "youtube" => array(
                "width" =>  '560',
                "hieght" => '315'
            ),
        );
    
        preg_match_all($regex, $content, $n);

        if(!is_null($n[0]))
            foreach($n[0] as $index=>$url){
                $content = str_replace($url, '<iframe width="'.$player_options["youtube"]["width"].'" height="'.$player_options["youtube"]["hieght"].'" src="http://www.youtube.com/embed/'.$n[1][$index].'" frameborder="0" allowfullscreen></iframe>', $content);
            }
        return $content;
    }
}